/**
 * Created by meiguizhinian on 2018/7/4
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
ivy.UICheckBox = ivy.UIBase.extend({
	_className:"ivy.UICheckBox",
	ctor: function (formName, blockName, sourceControlData) {
		this._super();
		this._touchBeginItemPos = cc.p(0, 0);
		this._selectActionID = new ivy.IDBaseT();
		this._unSelectActionID = new ivy.IDBaseT();
		this._onCheckChanged = new ivy.Delegate();
		if (formName && blockName && sourceControlData) {
			this.initWith(formName, blockName, sourceControlData);
		}
	},
	initWith : function(formName, blockName, sourceControlData) {
		let uim = ivy.UIManager.getInstance();
		let aniIDInUI = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eBgAniID);
		let actionIDInUI = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eBgActionID);
		let seqIDInUI = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eBgSeqID);
		let aniIDInUISel = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eSelBgAniID);
		let actionIDInUISel = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eSelBgActionID);
		let seqIDInUISel = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eSelBgSeqID);
		let x = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosX);
		let y = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosY);
		let w = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eWidth);
		let h = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eHeight);
		let layerID = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eLayerID);
		this._layerIDInEditor = layerID;
		let aniFileNameUnChecked = ivy.UIManager.getInstance().getAniFileNameByUIAniID(aniIDInUI);
		let aniFileNameChecked= ivy.UIManager.getInstance().getAniFileNameByUIAniID(aniIDInUISel);
		this._showType = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniCheckBox_ShowType_list]);
		if (actionIDInUI >= 0) {
			this._unCheckAniPlayer = ivy.AniPlayer.create(aniFileNameUnChecked, actionIDInUI, seqIDInUI === -1 ? 0 : seqIDInUI, true, seqIDInUI === -1 ?  ivy.AniPlayState.eCircle : ivy.AniPlayState.ePause);
			this.addChild(this._unCheckAniPlayer, 0);
		}
		if(actionIDInUISel>=0) {
			this._checkedAniPlayer = ivy.AniPlayer.create(aniFileNameChecked, actionIDInUISel, seqIDInUISel === -1 ? 0 : seqIDInUISel, true, seqIDInUISel === -1 ? ivy.AniPlayState.eCircle : ivy.AniPlayState.ePause);
			this.addChild(this._checkedAniPlayer, 1);
		}
		this.setLocalZOrder(layerID);
		this.setSrcRect(cc.rect(x, y, w, h));
		this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(x + w / 2.0, y + h / 2.0)));
		this.setSrcPosition(this.getPosition());
		this.setContentSize(cc.size(w, h));
		let anchorType = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniCheckBox_Anchor_list]);
		this.setAnchorType(anchorType);
		this._enterActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniCheckBox_EnterAction_action]));
		this._outActionID .setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniCheckBox_OutAction_action]));
		let defaultChecked = uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniCheckBox_DefaultChecked_list])===1;

		this._selectActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniCheckBox_SelectAction_action]));
		this._unSelectActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniCheckBox_UnselectAction_action]));
		this.setChecked(defaultChecked, true);

		this._enableUserTouch = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniCheckBox_EnableTouch_list])===1;

		let listener = cc.EventListener.create({
			event : cc.EventListener.TOUCH_ONE_BY_ONE,
			swallowTouches : true,
			onTouchBegan : this.onTouchBegan.bind(this),
			onTouchMoved : this.onTouchMoved.bind(this),
			onTouchEnded : this.onTouchEnded.bind(this),
		});
		cc.eventManager.addListener(listener, this);
		this.setSwallowTouches(true);
		this.setUIElementName(formName, blockName);
		//////////////////////////////////////////////////////////////////////////
		//设置初始可视性 wsq-2017-3-7
		let isNotVisble = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eNotVisble);
		if (isNotVisble === 1) {
			this.setVisible(false);
		}
	},
	setCheckedForce : function(checked) {
		this.setChecked(checked, false, false);
	},
	setCheckedWithoutChangeCallFunc : function(checked) {
		if (checked === this._isChecked)
			return;
		this.setChecked(checked, false, true);
	},

	/**
	 * void setChecked(bool checked);
	 * void setChecked(bool checked, isFirstTime, withoutCallChangeFunc=false);
	 */
	setChecked : function(checked, isFirstTime, withoutCallChangeFunc=false) {
		if (isFirstTime === undefined) {
			if (checked === this._isChecked) {
				return;
			}
			isFirstTime = false;
			withoutCallChangeFunc = false;
		}
		this._isChecked = checked;
		switch (this._showType) {
			case ivy.UICheckBox.UICheckBoxShowType.TwoPartsAniShow:
				if (this._checkedAniPlayer) {
					this._checkedAniPlayer.setVisible(this._isChecked);
				}
				if (this._unCheckAniPlayer) {
					this._unCheckAniPlayer.setVisible(!this._isChecked);
				}
				break;
			case ivy.UICheckBox.UICheckBoxShowType.SelectAniShow:
				if (this._checkedAniPlayer) {
					this._checkedAniPlayer.setVisible(this._isChecked);
				}
				if (this._unCheckAniPlayer) {
					this._unCheckAniPlayer.setVisible(true);
				}
				break;
			default:
				break;
		}
		if (!withoutCallChangeFunc) {
			this._onCheckChanged.call(this);
		}

		let runSelOrUnSelAction = false;
		if (isFirstTime && this._isChecked) {
			runSelOrUnSelAction = true;
		} else if (!isFirstTime) {
			runSelOrUnSelAction = true;
		}
		if (runSelOrUnSelAction) {
			if (this._isChecked) {
				if (this._selectActionID.valid()) {
					let action = ivy.CocosActionDataManager.getInstance().getGameActionBy(this._selectActionID);
					this.runAction(cc.sequence(
						cc.callFunc(function () {this.setIgnoreTouchEventWhenActionRunning(true);}, this),
						action,
						cc.callFunc(function () {this.setIgnoreTouchEventWhenActionRunning(false);}, this)
					));
				}
			} else {
				if (this._unSelectActionID.valid()) {
					let action = ivy.CocosActionDataManager.getInstance().getGameActionBy(this._unSelectActionID);
					this.runAction(cc.sequence(
						cc.callFunc(function () {this.setIgnoreTouchEventWhenActionRunning(true);}, this),
						action,
						cc.callFunc(function () {this.setIgnoreTouchEventWhenActionRunning(false);}, this)
					));
				}
			}
		}
	},
	isChecked : function() {
		return this._isChecked;
	},
	onCheckChanged : function() {
		return this._onCheckChanged;
	},
	setCheckBoxType : function(type) {
		this._checkBoxType = type;
	},
	setAnimationID : function(aniID, aniplayerIndex) {
		//此处只要重新指定aniID将对旧动画进行释放 新建动画
		//主要是考虑到当动画类型不同切片和spine动画切换时的通用性
		let player = null;
		switch (aniplayerIndex) {
			case 0:
				player = this._unCheckAniPlayer;
				break;
			case 1:
				player = this._checkedAniPlayer;
				break;
			default:
				return;
		}
		let ischecked = this.isChecked();
		if (player) {
			let willReleaseAniPlayer = player;
			ivy.Tools.pushFunctionToCocosThread(function() {
				willReleaseAniPlayer.removeFromParent();
			});
		}
		switch (aniplayerIndex) {
			case 0:
				this._unCheckAniPlayer = new ivy.AniPlayer.create(aniID);
				this._unCheckAniPlayer.setCascadeColorEnabled(true);
				this._unCheckAniPlayer.setCascadeOpacityEnabled(true);
				this.addChild(this._unCheckAniPlayer,0);
				this.setChecked(ischecked);
				//setAnchorType(anchorType_);
				break;
			case 1:
				this._checkedAniPlayer = new ivy.AniPlayer.create(aniID);
				this._checkedAniPlayer.setCascadeColorEnabled(true);
				this._checkedAniPlayer.setCascadeOpacityEnabled(true);
				this.addChild(this._checkedAniPlayer,1);
				this.setChecked(ischecked);
				//setAnchorType(anchorType_);
				break;
			default:
				break;
		}
	},
	getAniPlayer : function(index) {
		switch (index) {
			case ivy.UICheckBox.UICheckBoxAniIndex.eUnCheckAni: {
				return this._unCheckAniPlayer;
			}
			case ivy.UICheckBox.UICheckBoxAniIndex.eCheckedAni: {
				return this._checkedAniPlayer;
			}
		}
		return null;
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////protected:
	setAnchorType : function(type) {
		this._super(type);
		let rect;
		let size;
		if (!this.getParent()) {
			rect = cc.rect(this.getSrcRect());
			size = cc.size(rect);
		} else {
			rect = cc.rect(this.getSrcRect());
			size = cc.size(rect);
			let parent = this.getParent();
			if (parent instanceof ivy.UIBase)
			{
				let rect1 = parent.getSrcRect();
				if (rect1.x===0 && rect1.y===0 && rect1.width === 0 && rect1.height === 0) {
				} else {
					let pos22 = cc.p(rect1.x + rect1.width / 2, rect1.y + rect1.height / 2);
					rect.x -= pos22.x;
					rect.y -= pos22.y;
					rect.y = -rect.y;
					let p = ivy.Tools.ConvertPointFromGL(cc.p(rect));
					rect.x = p.x;
					rect.y = p.y;
				}
			}
		}

		this.setContentSize(size);
		switch (type) {
			case ivy.UIBase.eAnchorLT://ok
				if (this._unCheckAniPlayer)
					this._unCheckAniPlayer.setPosition(size.width / 2.0, -size.height / 2.0);
				if (this._checkedAniPlayer)
					this._checkedAniPlayer.setPosition(size.width / 2.0, -size.height / 2.0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x, rect.y)));
				break;
			case ivy.UIBase.eAnchorLB://ok
				if (this._unCheckAniPlayer)
					this._unCheckAniPlayer.setPosition(size.width / 2.0, size.height / 2.0);
				if (this._checkedAniPlayer)
					this._checkedAniPlayer.setPosition(size.width / 2.0, size.height / 2.0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x, rect.y + size.height)));
				break;
			case ivy.UIBase.eAnchorRT://ok
				if (this._unCheckAniPlayer)
					this._unCheckAniPlayer.setPosition(-size.width / 2.0, -size.height / 2.0);
				if (this._checkedAniPlayer)
					this._checkedAniPlayer.setPosition(-size.width / 2.0, -size.height / 2.0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width, rect.y)));
				break;
			case ivy.UIBase.eAnchorRB://ok
				if (this._unCheckAniPlayer)
					this._unCheckAniPlayer.setPosition(-size.width / 2.0, size.height / 2.0);
				if (this._checkedAniPlayer)
					this._checkedAniPlayer.setPosition(-size.width / 2.0, size.height / 2.0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width, rect.y + size.height)));
				break;
			case ivy.UIBase.eAnchorHT://ok untest
				if (this._unCheckAniPlayer)
					this._unCheckAniPlayer.setPosition(0, -size.height / 2.0);
				if (this._checkedAniPlayer)
					this._checkedAniPlayer.setPosition(0, -size.height / 2.0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width / 2.0, rect.y)));
				break;
			case ivy.UIBase.eANchorHB://ok untest
				if (this._unCheckAniPlayer)
					this._unCheckAniPlayer.setPosition(0, size.height / 2.0);
				if (this._checkedAniPlayer)
					this._checkedAniPlayer.setPosition(0, size.height / 2.0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width / 2.0, rect.y + size.height)));
				break;
			case ivy.UIBase.eAnchorLV://ok
				if (this._unCheckAniPlayer)
					this._unCheckAniPlayer.setPosition(size.width / 2.0, 0);
				if (this._checkedAniPlayer)
					this._checkedAniPlayer.setPosition(size.width / 2.0, 0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x, rect.y + size.height / 2.0)));
				break;
			case ivy.UIBase.eAnchorRV://ok
				if (this._unCheckAniPlayer)
					this._unCheckAniPlayer.setPosition(-size.width / 2.0, 0);
				if (this._checkedAniPlayer)
					this._checkedAniPlayer.setPosition(-size.width / 2.0, 0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + rect.width, rect.y + size.height / 2.0)));
				break;
			case ivy.UIBase.eAnchorHV://ok
			default:
				if (this._unCheckAniPlayer)
					this._unCheckAniPlayer.setPosition(0, 0);
				if (this._checkedAniPlayer)
					this._checkedAniPlayer.setPosition(0, 0);
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(rect.x + size.width / 2.0, rect.y + size.height / 2.0)));
				break;
		}
		this.setSrcPosition(this.getPosition());
	},
	onTouchBegan : function(touch, event) {
		if (!this.isVisible())
			return false;
		if (this.getActionByTag(ivy.UIBase.CommonActionTAG.eEnterAction) || this.getActionByTag(ivy.UIBase.CommonActionTAG.eOutAction)) {
			return false;
		}
		if (!this._enableUserTouch)
			return false;
		if (this.isIgnoreTouchEventWhenActionRunning())
			return false;
		let touchPoint = touch.getLocation();
		let touchRectInWorldTransform = this.getTouchRectInWorldTransform();
		if (cc.rectContainsPoint(touchRectInWorldTransform, touchPoint)) {
			this._isTouching = true;
			this._touchBeginItemPos = this.convertToWorldSpace(cc.p(0,0));
			if (this._touchEnterFunc)
				this._touchEnterFunc(this);
			return true;
		}
		return false;
	},
	onTouchMoved : function(touch, event) {

	},
	onTouchEnded : function(touch, event) {
		let touchEndPos = cc.director.convertToGL(touch.getLocation());
		let touchEndItemPos = this.convertToWorldSpace(cc.p(0,0));
		let touchPoint = touch.getLocation();
		let touchRectInWorldTransform = this.getTouchRectInWorldTransform();
		let uim = ivy.UIManager.getInstance();
		if (uim.isIgnoreScrollCheck()) {
			if (cc.rectContainsPoint(touchRectInWorldTransform, touchPoint)) {
				{
					if (this._touchEndFunc && this._isTouching) {
						this._touchEndFunc(this);
					}
					switch (this._checkBoxType) {
						case ivy.UICheckBox.UICheckBoxType.eNormal:
							this.setChecked(!this._isChecked);
							break;
						case ivy.UICheckBox.UICheckBoxType.eOwnedByGroup:
							if (!this._isChecked)
								this.setChecked(!this._isChecked);
							break;
						default:
							break;
					}
				}
			} else {
				if (this._touchCancelFunc)
					this._touchCancelFunc(this);
			}
		} else {
			if (Math.abs(this._touchBeginItemPos.x - touchEndItemPos.x) < ivy.UIBase.TOUCH_MIN_VAILD_DIS &&
				Math.abs(this._touchBeginItemPos.y - touchEndItemPos.y) < ivy.UIBase.TOUCH_MIN_VAILD_DIS &&
				cc.rectContainsPoint(touchRectInWorldTransform, touchPoint)) {
				if (this._touchEndFunc && this._isTouching) {
					this._touchEndFunc(this);
				}
				switch (this._checkBoxType) {
					case ivy.UICheckBox.UICheckBoxType.eNormal:
						this.setChecked(!this._isChecked);
						break;
					case ivy.UICheckBox.UICheckBoxType.eOwnedByGroup:
						if (!this._isChecked)
							this.setChecked(!this._isChecked);
						break;
					default:
						break;
				}
			} else {
				if (this._touchCancelFunc)
					this._touchCancelFunc(this);
			}
		}
		this._isTouching = false;
	},

	_showType : 0,
	_unCheckAniPlayer : null,
	_checkedAniPlayer : null,
	_isChecked : false,
	_touchBeginItemPos : null,//cocos2d::Point
	_isTouching : false,
	_selectActionID : null,//cc::GameActionID
	_unSelectActionID : null,//cc::GameActionID
	_onCheckChanged : null,//cc::Delegate<void, UIBase*>
	_checkBoxType : 0,
});

ivy.UICheckBox.create = function (formName, blockName, sourceControlData) {
	return new ivy.UICheckBox(formName, blockName, sourceControlData);
};

ivy.UICheckBox.UICheckBoxType = {
	eNormal         : 0,
	eOwnedByGroup   : 1,
};

ivy.UICheckBox.UICheckBoxShowType = {
	TwoPartsAniShow : 0,
	SelectAniShow   : 1,
};

ivy.UICheckBox.UICheckBoxAniIndex = {
	eUnCheckAni : 0,
	eCheckedAni : 1,
};